package com.globallogic.weeks;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Day of week calculation
 */
public class Weeks {
    private static enum fmtDate {DD, MM, YYYY}

    public static void main(String [] args) {
        try {
            System.out.print("Input date (DD.MM.YYYY): ");
            BufferedReader bReader = new BufferedReader (new InputStreamReader(System.in));
            String strDate = bReader.readLine();

            System.out.println("---------------------");
            System.out.println("Day of Week: " + dayOfWeek(extract(strDate, fmtDate.DD),
                                                           extract(strDate, fmtDate.MM),
                                                           extract(strDate, fmtDate.YYYY)));
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    private static int extract(String strDate, fmtDate fDate) {
        char separator = '.';
        int result = -1;

        if (fDate.equals(fmtDate.DD)) {
            result = Integer.parseInt(strDate.substring(0, strDate.indexOf(separator)));
        } else if (fDate.equals(fmtDate.MM)) {
            result = Integer.parseInt(strDate.substring(strDate.indexOf(separator) + 1, strDate.lastIndexOf(separator)));
        } else if (fDate.equals(fmtDate.YYYY)) {
            result = Integer.parseInt(strDate.substring(strDate.lastIndexOf(separator) + 1));
        }
        return result;
    }

    /**
     *  http://ru.wikibooks.org/wiki/Алгоритм_вычисления_дня_недели
     * @param day - Integer value: day of month.
     * @param month - Integer value: number manth of year.
     * @param year - Integer value: year.
     * @return - String: day of week.
     */
    private static String dayOfWeek(int day, int month, int year) {
        String [] week = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
        // a, y, m - values of calculation;
        int a = (int)(14-month)/12;
        int y = year - a;
        int m = month + 12 * a - 2;

        int idx = (7000 + (int)(day + y + (int)(y/4) - (int)(y/100) + (int)(y/400) + (31*m)/12))%7;
        return week[idx];
    }
}